logo头像
Snippet 博客主题

1.2 mongoDB install

本文于619天之前发表,文中内容可能已经过时。

mac 平台

1 Homebrew install

Homebrew
打开网页 copy

1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

粘贴到命令行
安装完成

2 mongoDB install

mongodb
mongodb下载

1
2
3
4
5
6
7
8

检验安装是否安装 mongo

开启 数据库 mongod
mongod -dbpath 路径
mongod -dbpath /Users/kylinhuang/learn/db

-dbpath 数据库文档位置

3 mongodb 常用命名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 使用数据库
mongo

# 列出所有的数据库
show dbs

# 使用某个数据库/新建数据库
use 数据库名

# 查看 当前所在数据库
db

# 插入数据
db.student.insert({"name":"kylinhuang","age":25});

# 查看 当前数据库所有集合
show collections

# 查询 student 中所有数据
db.student.find();

# 删除 数据库
db.dropDatabase();

3.1 插入数据 - 增

1
db.student.insert({"name":"xiaoming"});

3.2 删除数据 - 删

1
db.restaurants.remove( { "borough": "Manhattan" } )

By default, the remove() method removes all documents that match the remove condition. Use the justOne option to limit the remove operation to only one of the matching documents.

1
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )

3.3 修改数据 - 改

修改里面还有查询条件。你要该谁,要告诉mongo。
查找名字叫做小明的,把年龄更改为16岁:

1
db.student.update({"name":"小明"},{$set:{"age":16}});

查找数学成绩是70,把年龄更改为33岁:

1
db.student.update({"score.shuxue":70},{$set:{"age":33}});

更改所有匹配项目:
By default, the update() method updates a single document. To update multiple documents, use the multi option in the update() method.

1
db.student.update({"sex":"男"},{$set:{"age":33}},{multi: true});

完整替换,不出现$set关键字了:

1
db.student.update({"name":"小明"},{"name":"大明","age":16});

3.4 查找数据 - 查

查找数据,用find。find中没有参数,那么将列出这个集合的所有文档:

1
db.restaurants.find()

精确匹配:

1
db.student.find({"score.shuxue":70});

多个条件:

1
db.student.find({"score.shuxue":70 , "age":12})

大于条件:

1
db.student.find({"score.yuwen":{$gt:50}});

寻找所有年龄是9岁,或者11岁的学生

1
db.student.find({$or:[{"age":9},{"age":11}]});

查找完毕之后,打点调用sort,表示升降排序

1
db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )

4 导入数据

1
mongoimport --db test --collection restaurants --drop --file primer-dataset.json

–db test 想往哪个数据库里面导入
–collection restaurants 想往哪个集合中导入
–drop 把集合清空
–file primer-dataset.json 哪个文件
这样,我们就能用sublime创建一个json文件,然后用mongoimport命令导入,这样学习数据库非常方便。

Robomongo 和 Mongochef

Robomongo 是一个基于 Shell 的跨平台开源 MongoDB 可视化管理工具,支持 Windows、Linux 和 Mac,嵌入了 JavaScript 引擎和 MongoDB mongo,只要你会使用 mongo shell,你就会使用 Robomongo,它还提了供语法高亮、自动补全、差别视图等。

下载地址
https://robomongo.org/download

MongoChef
MongoChef 是另一款强大的 MongoDB 可视化管理工具,支持 Windows、Linux 和 Mac。

MongoChef 下载地址,我们选择左侧的非商业用途的免费版下载。
https://studio3t.com/#mongochef-download-compare

支付宝打赏 微信打赏

打赏